<?php
include_once("../includes/common.php");
include_once("../class/User.class.php");
include_once("../class/Explore.class.php");
require_once("../islogin.php");

if ($islogin!=1){
    header('Location: /login.php');
    exit;
}
$pageName = "首页";
$menuavatar = !$user_info["message"]["avatar"] ? "/data/avatar/default.jpg" : $user_info["message"]["avatar"];


//随机bird图 不重复
$directory = 'static/blue_bird/';
$image_count = 8; //图片数量
// 检查session中是否存在已输出的图片
if (!isset($_SESSION['blue_bird'])) {
    $_SESSION['blue_bird'] = range(1, $image_count);
}
if (empty($_SESSION['blue_bird'])) {
    $_SESSION['blue_bird'] = range(1, $image_count);
}

shuffle($_SESSION['blue_bird']); // 打乱数组
$selected_image_num = array_pop($_SESSION['blue_bird']); // 取出最后一张图片

$blue_bird_image_path = $directory . $selected_image_num . '.png';


//获得点赞数
$likenum = $user->getUserLikeNum($user_info['message']['user_id'], 0);
$collect = $user->getUserLikeNum($user_info['message']['user_id'], 1);
$like_coll_num = $likenum+$collect;

//刷新统计量
$user->updateUStatistLike($user_info['message']['user_id'],$like_coll_num);

//获得粉丝比例性别
$fans = json_decode($user->getUserFans($user_info['message']['user_id'], 1,575732022),true);
$man_fans = $fans['male_count'];
$female_fans = $fans['female_count'];
$all_fans_num = $man_fans+$female_fans;

//刷新统计量
$user->updateUStatistfans($user_info['message']['user_id'],$all_fans_num);

$user_statistics = json_decode($user->user_statistics($user_info['message']['user_id']), true);

if ($like_coll_num > 0&& $all_fans_num > 0) {
    $like_ratio = $likenum / $like_coll_num * 100; // 点赞比例
    $collect_ratio = $collect / $like_coll_num * 100; // 收藏比例

    $man_ratio = number_format($man_fans / $all_fans_num * 100, 2);
    $female_ratio = number_format($female_fans / $all_fans_num * 100, 2);

} else {
    $like_ratio = 0;
    $collect_ratio = 0;

    $man_ratio = 0;
    $female_ratio = 0;
}

$allnote_num = $user->getUserNoteNum($user_info['message']['user_id']);

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <?php require_once('head.php'); ?>
</head> 
<body>
    
<!--==================== Preloader Start ====================-->
  <div class="preloader">
    <div class="loader"></div>
  </div>
<!--==================== Preloader End ====================-->

<!--==================== Sidebar Overlay End ====================-->
<div class="side-overlay"></div>
<!--==================== Sidebar Overlay End ====================-->


    <?php require_once('user_menu.php'); ?>

    <div class="dashboard-main-wrapper">


        <?php require_once('top_navbar.php'); ?>
        
        <div class="dashboard-body">

            <div class="row gy-4">
                <div class="col-xxl-8">
                    <div class="card h-100">
                        <div class="card-body grettings-box-two position-relative z-1 p-0">
                            <div class="row align-items-center h-100">
                                <div class="col-lg-6">
                                    <div class="grettings-box-two__content">
                                        <h2 class="fw-medium mb-0 flex-align gap-10">欢迎<?php
                                            switch ($user_info["message"]["u_type"]) {
                                                case 1:
                                                    echo "管理员";
                                                    break;
                                                case 2:
                                                    echo "用户";
                                                    break;
                                                case 3:
                                                    echo "审查官";
                                                    break;
                                                case 0:
                                                    echo "机器人";
                                                    break;
                                                default:
                                                    echo "未知类型";
                                            }
                                            ?>
                                            ，<?php echo $user_info["message"]["nickname"];?> <img src="static/picture/wave-hand.png" alt=""> </h2>

                                        <h2 class="fw-medium mb-16"></h2>
                                        <p class="text-15 text-gray-400">笔记：<?php echo $allnote_num;?> 篇</p>

                                        <h2 class="fw-medium mb-16"></h2>
                                        <p class="text-15 text-gray-400">本次登录IP：<?php echo long2ip($user_info["message"]["ip"]);?></p>

                                        <h2 class="fw-medium mb-16"></h2>
                                        <p class="text-15 text-gray-400">登录地区：<?php echo getIpProvince($user_info["message"]["ip"]);?></p>
                                        <a href="edit.php" class="btn btn-main rounded-pill mt-32">发布笔记</a>
                                    </div>
                                </div>
                                <div class="col-lg-6 d-md-block d-none mt-auto">
                                    <img src="<?php echo $blue_bird_image_path;?>" alt="">
                                </div>
                            </div>
                            <img src="static/image/star-shape.png" class="position-absolute start-0 top-0 w-100 h-100 z-n1 object-fit-contain" alt="">
                        </div>
                    </div>
                </div>
                <div class="col-xxl-4">
                    <!-- Widgets Start -->
                    <div class="row gy-4">
                        <div class="col-sm-12">
                            <div class="card">
                                <div class="card-body text-center">
                                    <div class="flex-center gap-8 mb-24">
                                        <span class="flex-shrink-0 w-48 h-48 flex-center rounded-circle bg-main-600 text-white text-2xl"><i class="ph-fill ph-users-three"></i></span>
                                    </div>

                                    <h4 class="mb-2">拥有 <?php echo $user_statistics['message']['fans_count']?> 位</h4>
                                    <span class="text-gray-300">粉丝</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-12">
                            <div class="card">
                                <div class="card-body text-center">
                                    <div class="flex-center gap-8 mb-24">
                                        <span class="flex-shrink-0 w-48 h-48 flex-center rounded-circle bg-success-500 text-white text-2xl"><i class="ph-fill ph-eye"></i></span>
                                    </div>

                                    <h4 class="mb-2">关注 <?php echo $user_statistics['message']['following_count']?> 个</h4>
                                    <span class="text-gray-300">用户</span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- Widgets End -->
                </div>
            </div>
            
            
            <div class="mt-24">
                <div class="row gy-4">
                    
                    <div class="col-xxl-6">
                        <!-- Progress Bar Start -->
                        <div class="card h-100">
                            <div class="card-header border-bottom border-gray-100 flex-between flex-wrap gap-8">
                                <h5 class="mb-0">笔记 赞/收藏比例</h5>
                                <div class="dropdown flex-shrink-0">
                                    <button class="text-gray-400 text-xl d-flex rounded-4" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                                        <i class="ph-fill ph-dots-three-outline"></i>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu--md border-0 bg-transparent p-0">
                                        <div class="card border border-gray-100 rounded-12 box-shadow-custom">
                                            <div class="card-body p-12">
                                                <div class="max-h-200 overflow-y-auto scroll-sm pe-8">
                                                    <ul>
                                                        <li class="mb-0">
                                                            <a href="userlist.php?type=like" class="py-6 text-15 px-8 hover-bg-gray-50 text-gray-300 w-100 rounded-8 fw-normal text-xs d-block text-start">
                                                                <span class="text"> <i class="ph ph-book-open-user me-4"></i> 详情</span>
                                                            </a>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="card-body">
    
                               <div class="">
                                    <span class="text-gray-200 text-lg mb-12 mt-10 d-block text-center">赞+收藏总数量</span>
                                    <h1 class="text-32 fw-medium mb-12 text-center">收获 <?php echo $user_statistics['message']['likes_received_count']?> 个赞和收藏</h1>
                                    <div class="flex-between flex-wrap">
                                        <div class="d-flex gap-8 mt-12 flex-column w-50-perc pe-8">
                                            <div class="progress w-100 bg-main-100 rounded-pill h-4" role="progressbar" aria-label="Basic example" aria-valuenow="32" aria-valuemin="0" aria-valuemax="100">
                                                <div class="progress-bar bg-main-600 rounded-pill" style="width: <?php echo round($like_ratio, 2);?>%"></div>
                                            </div>
                                            <span class="text-neutral-600 flex-shrink-0 text-13 fw-medium"><?php echo round($like_ratio, 2);?>%</span>
                                        </div>
                                        <div class="d-flex gap-8 mt-12 flex-column w-50-perc ps-8">
                                            <div class="progress w-100 bg-warning-100 rounded-pill h-4" role="progressbar" aria-label="Basic example" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100">
                                                <div class="progress-bar bg-warning-600 rounded-pill" style="width: <?php echo round($collect_ratio, 2);?>%"></div>
                                            </div>
                                            <span class="text-neutral-600 flex-shrink-0 text-13 fw-medium"><?php echo round($collect_ratio, 2);?>%</span>
                                        </div>
                                    </div>
    
                                    <!-- Progress Card Start -->
                                    <div class="mt-20">
                                        <div class="bg-primary-50 rounded-16 p-12 flex-between flex-wrap gap-8 mb-16">
                                            <div class="flex-align gap-16">  
                                                <span class="w-48 h-48 rounded-8 flex-center text-xl bg-primary-600 text-white"><i class="ph ph-heart"></i></span>
                                                <h2 class="mb-0 text-22 fw-medium text-primary-600">收获 <?php echo $likenum;?> 个赞</h2>
                                            </div>
                                            <span class="px-16 py-4 rounded-pill bg-white border border-primary-600 text-primary-600 fw-medium">点赞量</span>
                                        </div>
                                        <div class="bg-warning-50 rounded-16 p-12 flex-between flex-wrap gap-8 mb-0">
                                            <div class="flex-align gap-16">  
                                                <span class="w-48 h-48 rounded-8 flex-center text-xl bg-warning-600 text-white"><i class="ph ph-star"></i></span>
                                                <h2 class="mb-0 text-22 fw-medium text-warning-600">被收藏了 <?php echo $collect;?> 次</h2>
                                            </div>
                                            <span class="px-16 py-4 rounded-pill bg-white border border-warning-600 text-warning-600 fw-medium">收藏数</span>
                                        </div>
                                    </div>
                                    <!-- Progress Card End -->
                               </div>
                            </div>
                        </div>
                        <!-- Progress bar end -->
                    </div>
    
                    <div class="col-xxl-6">
                        <!-- Donut Chart Start -->
                        <div class="card h-100">
                            <div class="card-header border-bottom border-gray-100 flex-between gap-8 flex-wrap">
                                <h5 class="mb-0">粉丝性别比</h5>
                                <div class="dropdown flex-shrink-0">
                                    <button class="text-gray-400 text-xl d-flex rounded-4" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                                        <i class="ph-fill ph-dots-three-outline"></i>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu--md border-0 bg-transparent p-0">
                                        <div class="card border border-gray-100 rounded-12 box-shadow-custom">
                                            <div class="card-body p-12">
                                                <div class="max-h-200 overflow-y-auto scroll-sm pe-8">
                                                    <ul>
                                                        <li class="mb-0">
                                                            <a href="userlist.php" class="py-6 text-15 px-8 hover-bg-gray-50 text-gray-300 w-100 rounded-8 fw-normal text-xs d-block text-start">
                                                                <span class="text"> <i class="ph ph-book-open-user me-4"></i> 详情</span>
                                                            </a>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="card-body">
                                <div class="flex-center mb-20">
                                    <div id="activityDonutChart" class="w-auto d-inline-block"></div>
                                </div>

                                <div class="flex-center gap-4 flex-wrap mt-20 justify-center">
                                    <div class="flex-center flex-column" style="margin-right: 100px">
                                        <span class="w-12 h-12 bg-white border border-3 border-main-600 rounded-circle"></span>
                                        <span class="text-13 my-4 text-main-600">男性</span>
                                        <h6 class="mb-0"><?php echo $man_ratio;?>%</h6>
                                    </div>
                                    <div class="flex-center flex-column">
                                        <span class="w-12 h-12 bg-white border border-3 border-danger-600 rounded-circle"></span>
                                        <span class="text-13 my-4 text-danger-600">女性</span>
                                        <h6 class="mb-0"><?php echo $female_ratio;?>%</h6>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- Donut Chart End -->
                    </div>
    
                </div>
            </div>
        </div>

        <?php require_once('footer.php'); ?>
    </div>
    <?php require_once('foot.php'); ?>
    <script>





        // =========================== Double Line Chart End ===============================

        // ============================ Donut Chart Start ==========================
        var options = {
            series: [<?php echo $man_ratio;?>, <?php echo $female_ratio;?>],
            chart: {
                height: 270,
                type: 'donut',
            },
            colors: ['#3D7FF9', '#EA5455'],
            enabled: true, // Enable data labels
            formatter: function (val, opts) {
                return opts.w.config.series[opts.seriesIndex] + '%';
            },
            dropShadow: {
                enabled: false
            },
            plotOptions: {
                pie: {
                    donut: {
                        size: '55%' // Fixed slice width
                    }
                }
            },
            responsive: [{
                breakpoint: 480,
                options: {
                    chart: {
                        width: "100%"
                    },
                    legend: {
                        show: false
                    }
                }
            }],
            legend: {
                position: 'right',
                offsetY: 0,
                height: 230,
                show: false
            }
        };

        var chart = new ApexCharts(document.querySelector("#activityDonutChart"), options);
        chart.render();
        // ============================ Donut Chart End ==========================
        
    </script>
    
    </body>
</html>